<?php
class PreguntasController extends AppController {

	var $name = 'Preguntas';
	var $components = array('Acl', 'RequestHandler', 'Session');
	
	function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->authorize = 'crud';
		$this->Auth->allowedActions = array('*');
		//$this->Auth->ajaxLogin= "tologin";
	}

	function index() {
		$this->Pregunta->recursive = 0;
		$this->set('preguntas', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(sprintf(__('Invalid %s', true), 'preguntum'));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('pregunta', $this->Pregunta->read(null, $id));
	}

	function add($cuestionario_id=null) {
		echo "Nro del cuestionario: ".$cuestionario_id;
		$this->set('cuestionario_id', $cuestionario_id);
		$cuestionarios = $this->Pregunta->Cuestionario->find('list');
		$tipos = $this->Pregunta->Tipo->find('list');
		$this->set(compact('cuestionarios', 'tipos', 'parentPreguntas'));
	}
	
	function formPregunta($cuestionario_id){
		$this->set('cuestionario_id', $cuestionario_id);
		$tipos = $this->Pregunta->Tipo->find('list');
		$this->set(compact('tipos'));
	}
	
	function addPregunta($cuestionario_id){
		if (!empty($this->data)) {
			$this->data['Pregunta']['cuestionario_id']=$cuestionario_id;
			$this->Pregunta->create();
			if ($this->Pregunta->save($this->data)) {
				echo "correcto";
				$this->Session->setFlash(sprintf(__('The %s has been saved', true), 'preguntum'));
				//$this->redirect(array('action' => 'index'));
			} else {
				echo "incorrecto el guardado";
				$this->Session->setFlash(sprintf(__('The %s could not be saved. Please, try again.', true), 'preguntum'));
			}
		}
		$preguntas = $this->Pregunta->find('all',array('cuestionario_id'=>$cuestionario_id));
		$this->set('preguntas', $preguntas);
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(sprintf(__('Invalid %s', true), 'preguntum'));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Pregunta->save($this->data)) {
				$this->Session->setFlash(sprintf(__('The %s has been saved', true), 'preguntum'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(sprintf(__('The %s could not be saved. Please, try again.', true), 'preguntum'));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Pregunta->read(null, $id);
		}
		$cuestionarios = $this->Pregunta->Cuestionario->find('list');
		$tipos = $this->Pregunta->Tipo->find('list');
		$parentPreguntas = $this->Pregunta->ParentPreguntum->find('list');
		$this->set(compact('cuestionarios', 'tipos', 'parentPreguntas'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(sprintf(__('Invalid id for %s', true), 'preguntum'));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Pregunta->delete($id)) {
			$this->Session->setFlash(sprintf(__('%s deleted', true), 'Preguntum'));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(sprintf(__('%s was not deleted', true), 'Preguntum'));
		$this->redirect(array('action' => 'index'));
	}
}
?>